define(function(require, exports, module) {
	"require:nomunge,exports:nomunge,module:nomunge";
	"use strict";

	var utils = require("mokit/utils");
	var linq = require('mokit/linq');
	var $ = require('mokit/jquery');
	var langMgr = require('mokit/language');
	var app = require('mokit/app');

	var self = exports;

	/**
	 * 从数组中查找第一个匹配的无素
	 */
	self.findFromArray = function(array, fn) {
		if ($utils.isNull(array)) return;
		var foundArray = linq.From(array).Where(fn).ToArray();
		if (foundArray != null && foundArray.length > 0) {
			return foundArray[0];
		} else {
			return null;
		}
	};

	/**
	 * 提示框（此函数会检查是否为 Cordova 环境，根据检查结果优选使用 Cordova 的提示框）
	 */
	self.alert = function(msg, callback, title, button) {
		//Cordova 提示框
		title = title || "提示";
		button = button || "确定";
		if (navigator && navigator.notification && navigator.notification.alert) {
			navigator.notification.alert(msg, callback, title, button);
		} else {
			//原生提示框
			utils.async(function() {
				alert(msg);
				if (callback) callback();
			}, 15);
		}
	};

	/**
	 * 确认提示框（此函数会检查是否为 Cordova 环境，根据检查结果优选使用 Cordova 的提示框）
	 */
	self.confirm = function(msg, callback, title, buttons) {
		//Cordova 提示框
		title = title || "提示";
		buttons = buttons || ["确定","取消"];
		if (navigator && navigator.notification && navigator.notification.confirm) {
			navigator.notification.confirm(msg, callback, title, buttons);
		} else {
			//原生提示框
			utils.async(function() {
				var buttonIndex = confirm(msg) ? 1 : 0;
				if (callback) callback(buttonIndex);
			}, 15);
		}
	};

	/**
	 * 输入提示框（此函数会检查是否为 Cordova 环境，根据检查结果优选使用 Cordova 的提示框）
	 */
	self.prompt = function(msg, callback, title, buttons) {
		var lang = langMgr.current();
		title = title || "输入";
		buttons = buttons || ["确定","取消"];
		if (navigator && navigator.notification && navigator.notification.prompt) {
			navigator.notification.prompt(msg, callback, title, buttons);
		} else {
			//原生提示框
			utils.async(function() {
				var rs = {};
				rs.input1 = prompt(msg);
				rs.buttonIndex = !utils.isNull(rs.input1) ? 1 : 0;
				if (callback) callback(rs);
			}, 15);
		}
	};

	/**
	 * 吐司提示框(duration: 'short', 'long',position: 'top', 'center', 'bottom')
	 */
	self.toast = function(message, duration, position) {
		if (window && window.plugins && window.plugins.toast) {
			duration = duration || 'short';
			position = position || 'center';
			window.plugins.toast.show(message, duration, position);
		} else {
			self.alert(message);
		}
	};

	/**
	 * 返回键动作队列
	 */
	self.backAction = [];

	/**
	 * 转到指定的 Controller
	 **/
	self.go = function(_to, _back) {
		if (_back) {
			self.backAction.push(function() {
				app.start(_back, 2);
			});
		}
		app.start(_to, 1);
	};

	/**
	 * 返回上一个 Controller
	 * */
	self.back = function(_back) {
		var action = self.backAction.pop();
		if (action) {
			action();
		} else if (_back) {
			app.start(_back, 2);
		}
	};

	/**
	 * 检查是否是iOS融合状态栏
	 */
	self.fusionStatusBar = function() {
		//return true;
		var device = window.device;
		return device && (device.platform == 'iOS') && (device.version > '6.0');
	};

	/**
	 * 状态栏占位符（DIV）
	 */
	self.fusionStatusBarSpace = function(size) {
		return self.fusionStatusBar() ? "<div class='ui-statusbar' data-role='cell' style='height:" + size + "px;'></div>" : "";
	};

	/**
	 * 获取设备信息
	 */
	self.getDevice = function() {
		return window.device || {};
	};

	/**
	 * 获取平台信息
	 */
	self.isPlatform = function(name, version) {
		var device = self.getDevice();
		var os = (device.platform == name);
		if (version) {
			return utils.startWith(device.version || '', version);
		} else {
			return os;
		}
	};

	/**
	 * 检查是否是安卓
	 */
	self.isAndroid = function(version) {
		return self.isPlatform('Android', version);
	};

	/**
	 * 检查是否是iOS
	 */
	self.isIOS = function(version) {
		return self.isPlatform('iOS', version);
	};

	/**
	 * 设置状态前景色
	 */
	self.setStatusBarColor = function(color) {
		var statusBar = window.StatusBar;
		if (statusBar == null) return;
		if (color == 'white') {
			statusBar.styleBlackTranslucent();
		} else {
			statusBar.styleDefault();
		}
	};

	/**
	 * 计算单行文本宽度
	 */
	self.getTextWidth = function(text, style) {
		if (!utils.isString(text) || text.length == 0) return 0;
		var styleStr = '';
		//处理对象格式的样式
		if (utils.isString(style)) {
			utils.each(style, function(key, value) {
				styleStr += key + ':' + value + ';';
			});
		} else {
			styleStr = style || '';
		}
		//计算宽度
		var box = $('<div style="width:0px;height:0px;overflow:hidden;position:fixed;left:0px;top:0px;"><pre style="display:table;display:inline-block;' + styleStr + '">' + text + '</pre></div>');
		$(document.body).append(box);
		var width = box.find('pre').width();
		box.remove();
		return width || 0;
	};

	/**
	 * 在对象中查找最长文件计算单行文本宽度
	 */
	self.getMaxTextWidth = function(target, prop, style, trimNum) {
		prop = prop || '--none--';
		if (utils.isString(target)) {
			return self.getTextWidth(target, style);
		} else if (utils.isArray(target)) {
			var text = '';
			utils.each(target, function(i, item) {
				if (!item) return;
				var val = (item[prop] || item || '');
				if (trimNum && utils.isNumber(val)) val = parseInt(val);
				val = val.toString();
				if (val.length > text.length) {
					text = val;
				}
			});
			return self.getTextWidth(text, style);
		} else if (utils.isObject(target) && target[prop]) {
			var val = target[prop];
			if (trimNum && utils.isNumber(val)) val = parseInt(val);
			return self.getTextWidth(val.toString(), style);
		} else {
			return 0;
		}
	};

	/**
	 * 移字符串的除占位符
	 */
	self.removePlaceHolder = function(str) {
		if (str) {
			str = str.replace(new RegExp('{.*?}', "g"), '');
		}
		return str;
	};
	//window.commonUtils = self;


	/*============add by Li at 2015.3.22========================*/

	//日期解析
	exports.parseDate = function(date) {
		if (typeof date == "string") {
			date = date.split('.')[0].replace(/\-/g, "/").replace('T', ' ');
		}
		if (typeof date != "object") {
			date = new Date(date);
		}
		return date;
	};

	//日期格式化
	exports.date_format = function(date, fmtString, flag) {
		date = this.parseDate(date);
		if (!date) {
			return "";
		}
		if (flag) {
			var s = new Date().getTime() / 1000 - date.getTime() / 1000;
			if (s > 0 && s < 3600) {
				return Math.ceil(s / 60) + "分钟前";
			} else if (s > 0 && s < 86400) {
				return Math.round(s / 3600) + "小时前";
			}
		}
		var e = {
			"M+": date.getMonth() + 1,
			"d+": date.getDate(),
			"h+": date.getHours(),
			"m+": date.getMinutes(),
			"s+": date.getSeconds(),
			"q+": Math.floor((date.getMonth() + 3) / 3),
			"S": date.getMilliseconds()
		};
		if (/(y+)/.test(fmtString)) {
			fmtString = fmtString.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
		}
		for (var d in e) {
			if (new RegExp("(" + d + ")").test(fmtString)) {
				fmtString = fmtString.replace(RegExp.$1, RegExp.$1.length == 1 ? e[d] : ("00" + e[d]).substr(("" + e[d]).length));
			}
		}
		return fmtString;
	};
});